#!/bin/bash

if [ ! -f util/.serverpod_util_root ]; then
    echo "Run this script from the root of the Serverpod repository"
    echo "I.e. util/update_pubspecs"
    exit 1
fi

# Makes script exit on first non-zero error code
set -e

VERSION=`cat SERVERPOD_VERSION`
DART_VERSION=`cat DART_VERSION`
FLUTTER_VERSION=`cat FLUTTER_VERSION`

BASE=`pwd`
CLI_DIR=$BASE/tools/serverpod_cli
CLI=$CLI_DIR/bin/serverpod_cli.dart

echo "pub get cli"
cd $CLI_DIR
dart pub get
cd $BASE

echo "Sort all pubspecs dependencies"
pubspecs=$(find . -name "pubspec.yaml" -type f)
echo "$pubspecs" | xargs -P $(util/get_max_cores) -I {} bash -c 'dart run sort_pubspec_dependencies --pubspec-path="{}" > /dev/null'

echo "Update all pubspecs. Version is $VERSION (Dart version: $DART_VERSION, Flutter version: $FLUTTER_VERSION)."

dart $CLI generate-pubspecs --version "$VERSION" --dart-version "$DART_VERSION" --flutter-version "$FLUTTER_VERSION" --mode development

echo "Copying CHANGELOG.md"
cp CHANGELOG.md packages/serverpod/CHANGELOG.md
cp CHANGELOG.md packages/serverpod_client/CHANGELOG.md
cp CHANGELOG.md packages/serverpod_serialization/CHANGELOG.md
cp CHANGELOG.md packages/serverpod_service_client/CHANGELOG.md
cp CHANGELOG.md packages/serverpod_shared/CHANGELOG.md
cp CHANGELOG.md packages/serverpod_flutter/CHANGELOG.md
cp CHANGELOG.md packages/serverpod_lints/CHANGELOG.md
cp CHANGELOG.md packages/serverpod_test/CHANGELOG.md

cp CHANGELOG.md templates/serverpod_templates/CHANGELOG.md
cp CHANGELOG.md tools/serverpod_cli/CHANGELOG.md
cp CHANGELOG.md tests/serverpod_test_client/CHANGELOG.md
cp CHANGELOG.md tests/serverpod_test_server/CHANGELOG.md
cp CHANGELOG.md tests/serverpod_test_shared/CHANGELOG.md
cp CHANGELOG.md tests/serverpod_new_auth_test/serverpod_new_auth_test_flutter/CHANGELOG.md
cp CHANGELOG.md tests/serverpod_new_auth_test/serverpod_new_auth_test_client/CHANGELOG.md
cp CHANGELOG.md tests/serverpod_new_auth_test/serverpod_new_auth_test_server/CHANGELOG.md
cp CHANGELOG.md tests/serverpod_test_module/serverpod_test_module_client/CHANGELOG.md
cp CHANGELOG.md tests/serverpod_test_module/serverpod_test_module_server/CHANGELOG.md
cp CHANGELOG.md tests/serverpod_test_nonvector/serverpod_test_nonvector_client/CHANGELOG.md
cp CHANGELOG.md tests/serverpod_test_nonvector/serverpod_test_nonvector_server/CHANGELOG.md
cp CHANGELOG.md tests/serverpod_test_flutter/CHANGELOG.md

cp CHANGELOG.md modules/legacy/serverpod_auth/serverpod_auth_server/CHANGELOG.md
cp CHANGELOG.md modules/legacy/serverpod_auth/serverpod_auth_client/CHANGELOG.md
cp CHANGELOG.md modules/legacy/serverpod_auth/serverpod_auth_shared_flutter/CHANGELOG.md
cp CHANGELOG.md modules/legacy/serverpod_auth/serverpod_auth_email_flutter/CHANGELOG.md
cp CHANGELOG.md modules/legacy/serverpod_auth/serverpod_auth_apple_flutter/CHANGELOG.md
cp CHANGELOG.md modules/legacy/serverpod_auth/serverpod_auth_google_flutter/CHANGELOG.md
cp CHANGELOG.md modules/legacy/serverpod_auth/serverpod_auth_firebase_flutter/CHANGELOG.md

cp CHANGELOG.md modules/new_serverpod_auth/serverpod_auth_core/serverpod_auth_core_flutter/CHANGELOG.md
cp CHANGELOG.md modules/new_serverpod_auth/serverpod_auth_core/serverpod_auth_core_client/CHANGELOG.md
cp CHANGELOG.md modules/new_serverpod_auth/serverpod_auth_core/serverpod_auth_core_server/CHANGELOG.md
cp CHANGELOG.md modules/new_serverpod_auth/serverpod_auth_idp/serverpod_auth_idp_server/CHANGELOG.md
cp CHANGELOG.md modules/new_serverpod_auth/serverpod_auth_idp/serverpod_auth_idp_client/CHANGELOG.md
cp CHANGELOG.md modules/new_serverpod_auth/serverpod_auth_idp/serverpod_auth_idp_flutter/CHANGELOG.md
cp CHANGELOG.md modules/new_serverpod_auth/serverpod_auth_bridge/serverpod_auth_bridge_client/CHANGELOG.md
cp CHANGELOG.md modules/new_serverpod_auth/serverpod_auth_bridge/serverpod_auth_bridge_flutter/CHANGELOG.md
cp CHANGELOG.md modules/new_serverpod_auth/serverpod_auth_bridge/serverpod_auth_bridge_server/CHANGELOG.md
cp CHANGELOG.md modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_client/CHANGELOG.md
cp CHANGELOG.md modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server/CHANGELOG.md

cp CHANGELOG.md modules/legacy/serverpod_chat/serverpod_chat_server/CHANGELOG.md
cp CHANGELOG.md modules/legacy/serverpod_chat/serverpod_chat_client/CHANGELOG.md
cp CHANGELOG.md modules/legacy/serverpod_chat/serverpod_chat_flutter/CHANGELOG.md

cp CHANGELOG.md integrations/serverpod_cloud_storage_s3/CHANGELOG.md
cp CHANGELOG.md integrations/serverpod_cloud_storage_gcp/CHANGELOG.md

echo "Copying README.md"
cp README.md packages/serverpod/README.md
cp README_subpackage.md packages/serverpod_client/README.md
cp README_subpackage.md packages/serverpod_serialization/README.md
cp README_subpackage.md packages/serverpod_service_client/README.md
cp README_subpackage.md packages/serverpod_shared/README.md
cp README_subpackage.md packages/serverpod_flutter/README.md
cp README_subpackage.md packages/serverpod_lints/README.md
cp README_subpackage.md packages/serverpod_test/README.md

cp README_subpackage.md templates/serverpod_templates/README.md
cp README_subpackage.md tools/serverpod_cli/README.md
cp README_subpackage.md tests/serverpod_test_client/README.md
cp README_subpackage.md tests/serverpod_test_server/README.md
cp README_subpackage.md tests/serverpod_test_shared/README.md
cp README_subpackage.md tests/serverpod_new_auth_test/serverpod_new_auth_test_flutter/README.md
cp README_subpackage.md tests/serverpod_new_auth_test/serverpod_new_auth_test_client/README.md
cp README_subpackage.md tests/serverpod_new_auth_test/serverpod_new_auth_test_server/README.md
cp README_subpackage.md tests/serverpod_test_module/serverpod_test_module_client/README.md
cp README_subpackage.md tests/serverpod_test_module/serverpod_test_module_server/README.md
cp README_subpackage.md tests/serverpod_test_nonvector/serverpod_test_nonvector_client/README.md
cp README_subpackage.md tests/serverpod_test_nonvector/serverpod_test_nonvector_server/README.md
cp README_subpackage.md tests/serverpod_test_flutter/README.md

cp README_subpackage.md modules/legacy/serverpod_auth/serverpod_auth_server/README.md
cp README_subpackage.md modules/legacy/serverpod_auth/serverpod_auth_client/README.md
cp README_subpackage.md modules/legacy/serverpod_auth/serverpod_auth_shared_flutter/README.md
cp README_subpackage.md modules/legacy/serverpod_auth/serverpod_auth_email_flutter/README.md
cp README_subpackage.md modules/legacy/serverpod_auth/serverpod_auth_apple_flutter/README.md
cp README_subpackage.md modules/legacy/serverpod_auth/serverpod_auth_google_flutter/README.md
cp README_subpackage.md modules/legacy/serverpod_auth/serverpod_auth_firebase_flutter/README.md

cp README_subpackage.md modules/new_serverpod_auth/serverpod_auth_core/serverpod_auth_core_flutter/README.md
cp README_subpackage.md modules/new_serverpod_auth/serverpod_auth_core/serverpod_auth_core_server/README.md
cp README_subpackage.md modules/new_serverpod_auth/serverpod_auth_core/serverpod_auth_core_client/README.md
cp README_subpackage.md modules/new_serverpod_auth/serverpod_auth_idp/serverpod_auth_idp_server/README.md
cp README_subpackage.md modules/new_serverpod_auth/serverpod_auth_idp/serverpod_auth_idp_client/README.md
cp README_subpackage.md modules/new_serverpod_auth/serverpod_auth_idp/serverpod_auth_idp_flutter/README.md
cp README_subpackage.md modules/new_serverpod_auth/serverpod_auth_bridge/serverpod_auth_bridge_client/README.md
cp README_subpackage.md modules/new_serverpod_auth/serverpod_auth_bridge/serverpod_auth_bridge_flutter/README.md
cp README_subpackage.md modules/new_serverpod_auth/serverpod_auth_bridge/serverpod_auth_bridge_server/README.md
cp README_subpackage.md modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_server/README.md
cp README_subpackage.md modules/new_serverpod_auth/serverpod_auth_migration/serverpod_auth_migration_client/README.md

cp README_subpackage.md modules/legacy/serverpod_chat/serverpod_chat_server/README.md
cp README_subpackage.md modules/legacy/serverpod_chat/serverpod_chat_client/README.md
cp README_subpackage.md modules/legacy/serverpod_chat/serverpod_chat_flutter/README.md

cp README_subpackage.md integrations/serverpod_cloud_storage_s3/README.md
cp README_subpackage.md integrations/serverpod_cloud_storage_gcp/README.md
